/**
 * <p>
 * 一个基于PopPanel的弹框控件，可以使背后的元素禁用
 * </p>
 * 
 * @class PopPanel的构造函数
 */

zxt.ui.PopPanel = function() {
	zxt.ui.Widget.apply(this, arguments);

	this.fns = [];
	this.afns = [];

	this.defaultTop = 0;
	this.defaultLeft = 0;

	this.defaultWidth = 600;
	this.defaultHeight = 400;
	this.defaultModal = true;
	this.defaultCaption = "";
	this.defaultScrolling = "auto";
	
	this.$SIZECHANGE = false;

	this._obj = false;
	this._dx = 0;
	this._dy = 0;
	this._x;
	this._y;
	
	this._frame = {
			"right":false,
			"bottom":false
	};
};

zxt.defineClazz(zxt.ui.PopPanel, zxt.ui.Widget);

/**
 * 返回控件类型
 * 
 * @function
 * @return {string} "zxt.ui.PopPanel"
 */
zxt.ui.PopPanel.prototype.getType = function() {
	return "zxt.ui.PopPanel";
};

/**
 * (string) [GETTER | SETTER] 对话框标题的名称
 *
 * @function
 */
zxt.ui.PopPanel.prototype.caption = function(c) {
	return this.fieldWithDefault("caption", c, this.defaultCaption);
};


/**
 * (string) [GETTER | SETTER] 对话框内容
 *
 * @function
 */
zxt.ui.PopPanel.prototype.content = function(c) {
	return this.fieldWithDefault("content", c, null);
};


/**
 * (string) [GETTER | SETTER] 对话框的宽度
 *
 * @function
 */
zxt.ui.PopPanel.prototype.width = function(w) {
	return this.fieldWithDefault("width", w, this.defaultWidth);
};


/**
 * (string) [GETTER | SETTER] 对话框的高度
 *
 * @function
 */
zxt.ui.PopPanel.prototype.height = function(h) {
	return this.fieldWithDefault("height", h, this.defaultHeight);
};


/**
 * (Boolean) [GETTER | SETTER] 对话框是否是模态对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.modal = function(m) {
	return this.fieldWithDefault("modal", m, this.defaultModal);
};


/**
 * (string) [GETTER | SETTER] 如果对话框是引用网址的话，这里是引用网址的url
 *
 * @function
 */
zxt.ui.PopPanel.prototype.url = function(u) {
	return this.fieldWithDefault("url", u, null);
};


/**
 * (jquery object) [GETTER | SETTER] 对话框的容器
 *
 * @function
 */
zxt.ui.PopPanel.prototype.getIframe = function() {
	return this.field("PopContainer");
};


/**
 * (Boolean) [GETTER | SETTER] 对话框的是否带滚动条
 *
 * @function
 */
zxt.ui.PopPanel.prototype.scrolling = function(s) {
	return this.fieldWithDefault("scrolling", s, this.defaultScrolling);
};


/**
 * (string) [GETTER | SETTER] 对话框距离浏览器左侧的距离
 *
 * @function
 */
zxt.ui.PopPanel.prototype.left = function(l) {
	return this.fieldWithDefault("left", l, this.defaultLeft);
};


/**
 * (string) [GETTER | SETTER] 对话框距离浏览器顶部的距离
 *
 * @function
 */
zxt.ui.PopPanel.prototype.top = function(t) {
	return this.fieldWithDefault("top", t, this.defaultTop);
};


/**
 * @private
 *
 * @function 显示对话框后面的遮盖层
 */
zxt.ui.PopPanel.prototype._showOverlay = function(){
	var overlay = this.field("PopOverlay");
	if (!overlay) {
		overlay = $("<div class='por-PopOverlay'></div>");

		this._window.$("body").append(overlay);
		this.field("PopOverlay", overlay);
	} else {
		overlay.show();
	}
	if (!this.modal()) {
		overlay.click(function() {
			instance.hide();
		});
	}
	this._fixIEDisplay(overlay);
};

/**
 * 渲染对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.render = function(){
	var instance = this;
	this._window = window//zxt.util.getTopWindow(window);
	var overlay = $("<div class='por-PopOverlay'></div>");
	this._window.$("body").append(overlay);
	overlay.hide();
	this.field("PopOverlay", overlay);
	var hide = this.field("PopHideSelect");
	
	// pop window
	var popWin = this.field("PopWindow");
	if (!popWin) {
		popWin = $("<div class='por-PopWindow'></div>");
		this._window.$("body").append(popWin);
		this.field("PopWindow", popWin);
	}
	
	
	var title = this.field("PopTitle");
	if (!title) {
		title = $("<div class='por-PopTitle'><div class='por-PopCaption'>"
				+ this.caption() + "</div></div>");
		title.mousedown(function(e) {
			instance._mousedown(e);
		});
		closeBtn = $("<div class='por-PopClose'>关闭</div>");
		closeBtn.click(function() {
			instance.hide();
		});
		title.append(closeBtn);
		this.field("PopClose", closeBtn);
		this.field("PopTitle", title);
		popWin.append(title);
	}
	title.find("div.por-PopCaption").html(this.caption());
	title.find("div.por-PopClose").width(40);
	var container = this.field("PopContainer");
	var content = this.content();
	var url = this.url();
	if (!container) {
		if (url != null) {// open new html page
			container = $("<iframe frameborder='0' hspace='0' class='por-PopIFrame' scrolling='auto'></iframe>");
			container[0].popPanel = this;
		} else {// open content
			container = $("<div class='por-PopContainer'></div>");
			container.append(content.show());
		}
		this.field("PopContainer", container);
		popWin.append(container);
	}
	if (this.modal()) {
		container.addClass("pop-PopModal");
	}
	if (url != null) {
		container.attr("width", this.width() - (-30));
		container.attr("height", this.height());
		var s = this.scrolling();
		s = s == false ? "no" : s == true ? "yes" : s;
		container.attr("scrolling", s);
	} else {
		container.width(this.width());
		container.height(this.height());
		
		//this.field("PopWindow").width(this.width());
		//this.field("PopWindow").height(this.height());
		
		var s = this.scrolling();
		switch (s) {
		case "scroll":
		case "hidden":
		case "auto":
		case "visible":
		case "inherit":
			break;
		default:
			s = "auto";
		}
		container.css("overflow", s);
	}

	var fn = function() {
		instance._fixIEDisplay(hide);
		instance._fixIEDisplay(overlay);
		instance._fixIEDisplay(popWin, 2);
	};
	$(window).resize(fn);
	this._fnGuid = fn.guid;

	this.position();
	// popWin.fadeIn();
	
	popWin.mousedown(function(e){
		instance._borderMousedown(e);
	});
	
	overlay.mousemove(function(e) {
		instance._mousemove(e);
	});
	popWin.mousemove(function(e) {
		instance._mousemove(e);
	});
	this._window.$("body").mouseup(function(e) {
		instance._mouseup(e);
	});
}

/**
 * 弹出／显示对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.show = function() {
	this._window = zxt.util.getTopWindow(window);
	if (window.parent != window) {
		this._window.$("body").addClass("bodyBackground");
	}
	// ie6
	if (typeof document.body.style.maxHeight === "undefined") {// if IE 6
		$("body", "html").css( {
			height : "100%",
			width : "100%"
		});
		$("html").css("overflow", "hidden");
		var hide = this.field("PopHideSelect");
		if (!hide) {
			hide = $("<iframe class='por-PopHideSelect'></iframe>");
			$("body").append(hide);
			this.field("PopHideSelect", hide);
		} else {
			hide.show();
		}
		this._fixIEDisplay(hide);
	}
	// overlay
	this._showOverlay();
	
	var popWin = this.field("PopWindow");
	popWin.css( {
		display : "block"
	});// .corner("15px top");
	this._fixIEDisplay(popWin, 2);
	var url = this.url();
	var content = this.content();
	if (url != null) {
		/*
		 * setTimeout(function(){ container.attr("src",url); },1);
		 */
		var container = this.field("PopContainer");
		container.attr("src", url);
	} else {
		if (content) {
			content.show();
			//container.append(content.show());
		}
	}
	this.isOpen = true;
};


/**
 * 隐藏对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.hide = function() {
	try {
		for ( var i = 0; i < this.fns.length; i++) {
			if (this.fns[i]) {
				if (this.fns[i].apply(this) === false) {
					return;
				}
			}
		}
	} catch (ex) {

	}
	var fn = function() {
	};
	fn.guid = this._fnGuid;
	$(window).unbind("resize", fn);
	if (this.url() != null) {
		var container = this.field("PopContainer");
		container.attr("src", "");
	} else {
		if (this.content()) {
			this.content().hide();
		}
	}
	var overlay = this.field("PopOverlay");
	var popWin = this.field("PopWindow");
	if (popWin) {
		popWin.css( {
			display : "none"
		});
		overlay.hide();
		// popWin.fadeOut("slow",function(){overlay.hide();});
		overlay.unbind("click");
	}
	if (typeof document.body.style.maxHeight == "undefined") {// if IE 6
		$("body", "html").css( {
			height : "auto",
			width : "auto"
		});
		$("html").css("overflow", "");
		var hide = this.field("PopHideSelect");
		if (hide) {
			hide.hide();
		}
	}
	this.isOpen = false;
	try {
		for ( var i = 0; i < this.afns.length; i++) {
			if (this.afns[i]) {
				this.afns[i].apply(this);
			}
		}
	} catch (ex) {

	}
	// document.onkeydown = "";
	return false;
};


/**
 * 移动对话框的位置
 * @param {string} left 对话框的左边距离浏览器左边缘的距离 单位像素
 * @param {string} top 对话框的上边距离浏览器上边缘的距离 单位像素
 * @function
 */
zxt.ui.PopPanel.prototype.moveTo = function(left, top) {
	this.left(left);
	this.top(top);
	this.position();
};


/**
 * 刷新对话框的位置
 *
 * @function
 */
zxt.ui.PopPanel.prototype.position = function(win) {
	var _win = win || this._window;
	var popWin = this.field("PopWindow");
	if (!popWin)
		return;
	
	this.defaultTop = (_win.document.documentElement.clientHeight / 2) - (this.height() / 2) - 30;
	this.defaultLeft = (_win.document.documentElement.clientWidth / 2) - (this.width() / 2) - 30;
	popWin.css("left", this.left());
	popWin.css("top", this.top());
};


/**
 * 关闭对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.close = function(fn, after) {
	if (typeof fn == "function") {
		if (!after) {
			this.fns.push(fn);
		} else {
			this.afns.push(fn);
		}
	} else {
		this.hide();
	}
	return this;
};


/**
 * 删除对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.remove = function() {
	this.destroy();
	var overlay = this.field("PopOverlay");
	var popWin = this.field("PopWindow");
	if (overlay) {
		overlay.unbind().remove();
	}
	if (popWin) {
		popWin.unbind().remove();
	}
};


/**
 * @private
 * 处理浏览器兼容性
 *
 * @function
 */
zxt.ui.PopPanel.prototype._fixIEDisplay = function(jq, _type) {
	if ($.browser.msie&&jq) {
		jq.css( {
			position : "absolute"
		});
	}
};


/**
 * @private
 * 销毁对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype.destroy = function() {
	var overlay = this.field("PopOverlay");
	var title = this.field("PopTitle");
	if (overlay) {
		overlay.unbind();
	}
	if (title) {
		title.find("div.por-PopClose").unbind();
	}
};


/**
 * @private
 * 销毁对话框
 *
 * @function
 */
zxt.ui.PopPanel.prototype._borderMousedown = function(e){
	if(!this.$SIZECHANGE){
		return;
	}
	var ps = zxt.diff.getPosition(e);
	var rightx = ps["X"]-this.left()-this.field("PopWindow").width();
	if(rightx>-8 && rightx<0){
		this._frame["right"] = true;
	}else{
		this._frame["right"] = false;
	};
	var bottomy = ps["Y"]-this.top()-this.field("PopWindow").height();
	if(bottomy>-8 && bottomy<0){
		this._frame["bottom"] = true;
	}else{
		this._frame["bottom"] = false;
	};
	this._Sizex = ps["X"];
	this._Sizey = ps["Y"];
	if(this._frame["right"]||this._frame["bottom"]){
		this._changeSizeSelected = true;
		this._obj = this;
	};
	
};


/**
 * @private
 * 调整对话框大小
 * @param {string} x 对话框增加的宽度 单位像素
 * @param {string} y 对话框增加的高度 单位像素
 * @function
 */
zxt.ui.PopPanel.prototype._changeSize = function(x,y){
	if(this._frame["right"]){
		this.field("PopWindow").width(this.field("PopWindow").width()+x);
	};
	if(this._frame["bottom"]){
		this.field("PopWindow").height(this.field("PopWindow").height()+y);
	};
};


/**
 * @private
 * 更改鼠标样式
 * @param {event} e 鼠标事件对象
 * @param {string} rightx 鼠标距离对话框右边框的距离 单位像素
 * @param {string} bottomy 鼠标距离对话框下边框的距离 单位像素
 * @function
 */
zxt.ui.PopPanel.prototype._changeMouseStyle = function(e,rightx,bottomy){
	if(!this.$SIZECHANGE){
		return;
	}
	if(rightx>-8 && rightx<0 && bottomy>-8 && bottomy<0){
		e.target.style.cursor= 'crosshair';
		return;
	}else if(bottomy>-8 && bottomy<0){
		e.target.style.cursor= 's-resize';
		return;
	}else if(rightx>-8 && rightx<0 ){
		e.target.style.cursor= 'e-resize';
		return;
	}else{
		e.target.style.cursor= 'default';
	};
};

/**
 * @private
 * 鼠标按下事件
 * @event
 */
zxt.ui.PopPanel.prototype._mousedown = function(e) {
	this._select = true;
	this._obj = this;
	this._dx = this._x - this._obj.left();
	this._dy = this._y - this._obj.top();
};

/**
 * @private
 * 鼠标移动事件
 * @event
 */
zxt.ui.PopPanel.prototype._mousemove = function(e) {
		var ps = zxt.diff.getPosition(e);
		
		if(ps == null){
			ps = {"X":this._x,"Y":this._y};
		}
		
		var rightx = ps["X"]-this.left()-this.field("PopWindow").width();
		var bottomy = ps["Y"]-this.top()-this.field("PopWindow").height();
		this._changeMouseStyle(e,rightx, bottomy);
		
		this._x = ps["X"];
		this._y = ps["Y"];
		if (this._obj&&this._select) {
			var x = this._x - this._dx;
			var y = this._y - this._dy;
			this.moveTo(x, y);
		}
		if(this._changeSizeSelected){
			var x = this._x - this._Sizex;
			var y = this._y - this._Sizey;
			this._changeSize(x, y);
			this._Sizex = this._x;
			this._Sizey = this._y;
		}
};

/**
 * @private
 * 鼠标释放事件
 * @event
 */
zxt.ui.PopPanel.prototype._mouseup = function(e) {
	if (!this._obj) {
		return;
	}
	this._frame["left"] = false;
	this._frame["right"] = false;
	this._frame["top"] = false;
	this._frame["bottom"] = false;
	this._changeSizeSelected = false;
	
	this._obj = false;
	this._select = false;
};
